<template>
    <div class="card">
        <div class="card-body">
            <div class="row">
        <div class="col-3 img-field" >
            <img class="img-fluid" :src="user.photo" alt=""> <!-- :表示变量 -->
        </div>


        <div class="col-9">
            <div class="username"> {{user.username}} </div>
            <div class="fans">粉丝: {{user.followerCount}}</div> <!-- v-on:可简写为@作用为为某个标签绑定某个事件 -->
            <button @click="follow" v-if="!user.is_followed" type="button" class="btn btn-primary btn-sm">关注</button>
            <button @click="unfollow" v-if="user.is_followed" type="button" class="btn btn-primary btn-sm">取消关注</button>
        </div>
    </div>
        </div>
    </div>
    
</template>

<script>
/* import { computed } from 'vue'; */
import $ from "jquery";
import { useStore } from "vuex";

export default {
    name:'UserProfileInfo',
    props: {
        user: {
            type: Object,
            required: true,
        },

        
    },

    setup(props, context) { // setup: 函数入口, props使用props中的变量，context:触发父组件即UserPrifileView中绑定的函数
        /* let fullName = computed(() => props.user.lastName + " " + props.user.firstName);
        
 */
        const store = useStore();
        const follow = () => {
            $.ajax ({
                url : "https://app165.acapp.acwing.com.cn/myspace/follow/",
                type: "POST",
                data: {
                    target_id: props.user.id,
                },
                headers: {
                    'Authorization' : "Bearer " + store.state.user.access,
                },

                success(resp) {
                    if (resp.result === "success"){
                        context.emit("follow"); // 触发UserProfileView中的follow函数
                    }
                }
            })
        };

        const unfollow = () => {
            $.ajax ({
                url : "https://app165.acapp.acwing.com.cn/myspace/follow/",
                type: "POST",
                data: {
                    target_id: props.user.id,
                },
                headers: {
                    'Authorization' : "Bearer " + store.state.user.access,
                },

                success(resp) {
                    if (resp.result === "success") {
                        context.emit("unfollow"); // 触发UserProfileView中的unfollow函数
                    }
                }
            })
        };

        return { // 只有返回了fullName上面的才可以调用fullName
           /*  fullName, */
            follow,
            unfollow
        }
    }
}

</script>

<style scoped>
img {
    border-radius: 50%;
}

.username {
    font-weight: bold;
}

.img-field { /* 竖直居中 */
    display: flex;
    flex-direction: column; /* 将主轴变为竖直方向 */
    justify-content: center;
}

.fans {
    font-size: 12px;
    color: grey;
}

button {
    padding: 2px 4px;
    font-size: 12px;
}
</style>